﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace app2
{
    public partial class Form2 : Form
    {
        MyBoll1 myboll;
        List<Boll1> bolls = new List<Boll1>();
        List<IBoll> ibolls = new List<IBoll>();
        List<LBoll> lbolls = new List<LBoll>();
        List<BBoll> bbolls = new List<BBoll>();
        SolidBrush sb1 = new SolidBrush(Color.Red);
        SolidBrush sb2 = new SolidBrush(Color.Blue);
        Image image1 = Image.FromFile(@"jinbid.png");//增加持续时间
        Image image2 = Image.FromFile(@"malad.png");//蓝色小球变小
        Image image3 = Image.FromFile(@"xing.png");//蓝色小球变大
        Image image4 = Image.FromFile(@"1.png");
        Image image5 = Image.FromFile(@"xmq.png");
        Graphics g = null;
        int bollnum = 10;
        public Random rand;
        public int timer = 0;
        public Form2()
        {
            InitializeComponent();
            this.DoubleBuffered = true;  //避免闪烁
        }

        private void Form2_Load(object sender, EventArgs e)
        {
            rand = new Random();
            myboll = new MyBoll1(this.ClientSize.Width, this.ClientSize.Height);
            for (int i = 0; i < bollnum; i++)
            {
                Boll1 boll = new Boll1(this.ClientSize.Width, this.ClientSize.Height, rand);
                bolls.Add(boll);
            }
            IBoll iboll = new IBoll(this.ClientSize.Width, this.ClientSize.Height, rand);
            ibolls.Add(iboll);
            LBoll lboll = new LBoll(this.ClientSize.Width, this.ClientSize.Height, rand);
            lbolls.Add(lboll);
            BBoll bboll = new BBoll(this.ClientSize.Width, this.ClientSize.Height, rand);
            bbolls.Add(bboll);
            timerball.Start();
            timertotal.Start();
        }

        private void Form2_Paint(object sender, PaintEventArgs e)
        {
            g = e.Graphics;
            foreach (Boll1 boll in bolls)
            {
                g.DrawImage(image4, boll.x, boll.y, boll.d, boll.d);
            }
            foreach (IBoll iboll in ibolls)
            {
                g.DrawImage(image1, iboll.x, iboll.y, iboll.d, iboll.d);
            }
            foreach (LBoll lboll in lbolls)
            {
                g.DrawImage(image2, lboll.x, lboll.y, lboll.d, lboll.d);
            }
            foreach (BBoll bboll in bbolls)
            {
                g.DrawImage(image3, bboll.x, bboll.y, bboll.d, bboll.d);
            }
            g.DrawImage(image5, myboll.x, myboll.y, myboll.d, myboll.d);
        }

        private void timerball_Tick(object sender, EventArgs e)
        {
            foreach (IBoll iboll in ibolls)
            {
                iboll.Move();
                double Y1 = myboll.y - iboll.y;
                double X1 = myboll.x - iboll.x;
                double R1 = X1 * X1 + Y1 * Y1;
                if (R1 < Math.Pow(iboll.d / 2 + myboll.d / 2, 2))
                {
                    timer = timer + 1;
                    Boll1 boll = new Boll1(this.ClientSize.Width, this.ClientSize.Height, rand);
                    bolls.Add(boll);
                }
            }
            foreach (LBoll lboll in lbolls)
            {
                lboll.Move();
                double Y1 = myboll.y - lboll.y;
                double X1 = myboll.x - lboll.x;
                double R1 = X1 * X1 + Y1 * Y1;
                if (myboll.d <= 0)
                    myboll.d = myboll.d + 1;
                if (R1 < Math.Pow(lboll.d / 2 + myboll.d / 2, 2))
                {
                    myboll.d = myboll.d - 1;
                    Boll1 boll = new Boll1(this.ClientSize.Width, this.ClientSize.Height, rand);
                    bolls.Add(boll);
                }
            }
            foreach (BBoll bboll in bbolls)
            {
                bboll.Move();
                double Y1 = myboll.y - bboll.y;
                double X1 = myboll.x - bboll.x;
                double R1 = X1 * X1 + Y1 * Y1;
                if (R1 < Math.Pow(bboll.d / 2 + myboll.d / 2, 2))
                {
                    myboll.d = myboll.d + 1;
                    Boll1 boll = new Boll1(this.ClientSize.Width, this.ClientSize.Height, rand);
                    bolls.Add(boll);
                }
            }
            foreach (Boll1 boll in bolls)
            {
                boll.Move();        
                double Y = myboll.y - boll.y;
                double X = myboll.x - boll.x;
                double R = X * X + Y * Y;
                if (R <Math.Pow(boll.d / 2 + myboll.d / 2,2))
                {
                    this.timerball.Stop();
                    this.timertotal.Stop();
                    MessageBox.Show("坚持了" + timer + "秒");
                    Database db = new Database();
                    string sql;
                    sql = "insert into time2 values ('" + timer + "')";
                    DataSet ds = new DataSet();
                    ds = db.GetDataFromDB(sql);
                    DialogResult a = MessageBox.Show("是否再次挑战？", "提示", MessageBoxButtons.OKCancel);
                    if (a == DialogResult.OK)
                    {
                        Form2 form = new Form2();
                        form.Show();
                        this.Hide();
                    }
                    else
                    {
                        Login login = new Login();
                        login.Show();
                        this.Hide();
                    }
                }
            }
            this.Refresh();
        }
        #region 控制球的方向，上下左右的操作
        private void Form2_KeyDown(object sender, KeyEventArgs e)
        {
            switch (e.KeyCode)
            {
                case Keys.Up:
                case Keys.W:
                    timerup.Start();
                    break;
                case Keys.Down:
                case Keys.S:
                    timerdown.Start();
                    break;
                case Keys.Left:
                case Keys.A:
                    timerleft.Start();
                    break;
                case Keys.Right:
                case Keys.D:
                    timerright.Start();
                    break;
                default: break;
            }
        }

        private void Form2_KeyUp(object sender, KeyEventArgs e)
        {
            switch (e.KeyCode)
            {
                case Keys.Up:
                case Keys.W:
                    timerup.Stop();
                    break;
                case Keys.Down:
                case Keys.S:
                    timerdown.Stop();
                    break;
                case Keys.Left:
                case Keys.A:
                    timerleft.Stop();
                    break;
                case Keys.Right:
                case Keys.D:
                    timerright.Stop();
                    break;
                default: break;
            }
        }

        private void timerup_Tick(object sender, EventArgs e)
        {
            myboll.Up();
        }

        private void timerleft_Tick(object sender, EventArgs e)
        {
            myboll.Left();
        }

        private void timerright_Tick(object sender, EventArgs e)
        {
            myboll.Right();
        }

        private void timerdown_Tick(object sender, EventArgs e)
        {
            myboll.Down();
        }
        #endregion

        private void timertotal_Tick(object sender, EventArgs e)
        {
            timer++;
        }
    }
}
